<?php

namespace app\wxapp\service;

use app\common\model\UserSign as UserSignModel;
use think\facade\Db;

/**
 * Class UserSignService
 * @package app\wxapp\service
 */
class UserSignService
{

    public function sign($uid)
    {
        if (!$this->isSignToday($uid)) {
            UserSignModel::create([
                'uid' => $uid
            ]);
        }
    }

    public function isSignToday($uid)
    {
        return UserSignModel::where(['create_time' => date('Y-m-d', time()), 'uid' => $uid])->count();
    }

    public function signCountTotal($uid)
    {
        return Db::table('user_sign')->where(['uid' => $uid])->count();
    }

    public function signCountMonth($uid)
    {
        $startTime = date('Y-m', time());
        $startTime = $startTime . '-0';
        $endTime = date('Y-m-d', time());
        return Db::table('user_sign')->where(['uid' => $uid])->whereBetweenTime('create_time', $startTime, $endTime)->count();
    }

    public function signMonthList($uid, $date)
    {
        $startDate = $date . '-1';
        $endDate = $date . date('-d', strtotime($date . ' +1month') - 1);
        $rows = Db::table('user_sign')->where(['uid' => $uid])->whereBetweenTime('create_time', $startDate, $endDate)->column('create_time') ?: [];
        $days = [];
        foreach ($rows as $row) {
            $days[] = strtotime($row);
        }
        return $days;
    }
}